﻿using System;
using System.Collections.Generic;
using Clipping.Infrastructure.Interfaces;
using Clipping.Infrastructure.Models;
using System.Text.RegularExpressions;
namespace Clipping.SearchEnginePageParsers
{
    public class GoogleNewsPageParser : ISearchEnginePageParse
    {
        public void Parse(string html, out List<News> news, out string nextPageUrl)
        {

            news = new List<News>();
            nextPageUrl = null;
            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            doc.LoadHtml(html);

            var liNav = doc.CreateNavigator();


            //普通搜索结果的提取


            var ol = liNav.Select("//ol[@id='rso']");
            if (ol != null)
            {
                var lis = liNav.Select("descendant::li[@class='g']");
                while (lis.MoveNext())
                {
                    News n = new News();
                    var li = lis.Current;

                    //取标题和链接
                    var linkNode = li.SelectSingleNode("descendant::a[@class='l']");
                    if (linkNode != null)
                    {
                        n.Title = linkNode.Value;
                        n.Url = linkNode.GetAttribute("href", "");
                    }


                    //取媒体
                    var mediaNode = li.SelectSingleNode("descendant::span[@class='news-source']");
                    if (mediaNode != null)
                    {
                        n.Media = mediaNode.Value;
                    }
                    //取时间
                    var dateNode = li.SelectSingleNode("descendant::span[@class='f nsa']");

                    if (dateNode != null)
                    {

                        string dateStr = dateNode.Value;
                        if (Regex.IsMatch(dateStr, @"\d+年\d+月\d+日"))
                        {
                            n.PubDate = Regex.Match(dateStr, @"(\d+年\d+月\d+日)").Groups[1].Value;
                        }
                        else
                        {
                            var hourMatch = Regex.Match(dateStr, @"(\d+)\s*小时前", RegexOptions.None);
                            if (hourMatch.Success)
                            {
                                string hoursStr = hourMatch.Groups[1].Value;
                                int hour = int.Parse(hoursStr);
                                n.PubDate = DateTime.Now.AddHours(0 - hour).ToShortDateString();
                            }
                            else
                            {
                                var minuteMatch = Regex.Match(dateStr, @"(\d+)\s*分钟前", RegexOptions.None);
                                if (minuteMatch.Success)
                                {
                                    string minuteStr = minuteMatch.Groups[1].Value;
                                    int minute = int.Parse(minuteStr);
                                    n.PubDate = DateTime.Now.AddMinutes(0 - minute).ToShortDateString();
                                }
                            }
                        }
                    }


                    //取摘要
                    var summaryNode = li.SelectSingleNode("descendant::div[@class='st']");
                    if (summaryNode != null)
                    {
                        n.Summary = summaryNode.Value;
                    }

                    var sameUrl = li.SelectSingleNode("descendant::a[@class='gl']");
                    if (sameUrl != null)
                    {
                        string uri = System.Web.HttpUtility.HtmlDecode(sameUrl.GetAttribute("href", ""));
                        uri = uri.Replace("more?", "story?");
                        if (Uri.IsWellFormedUriString(uri, UriKind.RelativeOrAbsolute))
                            n.SameNewsUrl = uri;
                    }
                    news.Add(n);
                }



                //相同新闻搜索结果的提取
                lis = liNav.Select("//div[contains(@class,'headline-story')]");
                while (lis.MoveNext())
                {
                    News n = new News();
                    var li = lis.Current;

                    //取标题和链接
                    var linkNode = li.SelectSingleNode("descendant::h2/a");
                    if (linkNode != null)
                    {
                        n.Title = linkNode.Value;
                        n.Url = linkNode.GetAttribute("href", "");
                    }

                    //取摘要
                    var summaryNode = li.SelectSingleNode("descendant::div[@class='snippet']");
                    if (summaryNode != null)
                    {
                        n.Summary = summaryNode.Value;
                    }

                    //取媒体
                    var mediaNode = li.SelectSingleNode("descendant::span[contains(@class,'source')]");
                    if (mediaNode != null)
                    {
                        n.Media = System.Web.HttpUtility.HtmlDecode(mediaNode.Value);
                    }
                    var dateNode = li.SelectSingleNode("descendant::span[@class='date']");
                    if (dateNode != null)
                    {

                        string dateStr = System.Web.HttpUtility.HtmlDecode(dateNode.Value);
                        if (Regex.IsMatch(dateStr, @"\d+年\d+月\d+日"))
                        {
                            n.PubDate = Regex.Match(dateStr, @"(\d+年\d+月\d+日)").Groups[1].Value;
                        }
                        else
                        {
                            var hourMatch = Regex.Match(dateStr, @"(\d+)\s*小时前", RegexOptions.None);
                            if (hourMatch.Success)
                            {
                                string hoursStr = hourMatch.Groups[1].Value;
                                int hour = int.Parse(hoursStr);
                                n.PubDate = DateTime.Now.AddHours(0 - hour).ToShortDateString();
                            }
                            else
                            {
                                var minuteMatch = Regex.Match(dateStr, @"(\d+)\s*分钟前", RegexOptions.None);
                                if (minuteMatch.Success)
                                {
                                    string minuteStr = minuteMatch.Groups[1].Value;
                                    int minute = int.Parse(minuteStr);
                                    n.PubDate = DateTime.Now.AddMinutes(0 - minute).ToShortDateString();
                                }
                            }
                        }
                    }


                    news.Add(n);
                }



                var aList = liNav.Select("descendant::a[@id='pnnext']");
                    if (aList != null)
                    {
                        while (aList.MoveNext())
                        {
                            var a = aList.Current;
                            if (a.Value.Contains("下一页"))
                            {
                                nextPageUrl = a.GetAttribute("href", "");
                                break;
                            }
                        }
                    }
                
             

            }
        }
    }
}
